package com.components.project

import android.os.Bundle
import android.util.Log
import androidx.appcompat.app.AppCompatActivity
import java.util.concurrent.Callable
import java.util.concurrent.Executors
import java.util.concurrent.Future

/**
 * @Author LuoKun
 * @Des
 */
class ThreadPoolActivity : AppCompatActivity() {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_thread_pool)
        initData()
    }

    private fun initData() {
        val cachedThreadPool = Executors.newSingleThreadExecutor()
        val submit: Future<*> = cachedThreadPool.submit {

        }
        val arrayListOf = arrayListOf<Callable<String>>(
            Callable {
                Log.e("ThreadPoolActivity", "task1 ${Thread.currentThread()}")
                return@Callable "任务一"
            },
            Callable {
                Log.e("ThreadPoolActivity", "task2 ${Thread.currentThread()}")
                Thread.sleep(10000)
                return@Callable "任务二"
            },
            Callable {
                Log.e("ThreadPoolActivity", "task3 ${Thread.currentThread()}")
                return@Callable "任务三"
            }
        )
        val invokeAll = cachedThreadPool.invokeAll(arrayListOf)
        invokeAll.forEach {
            Log.e("ThreadPoolActivity", "执行结果${it.get()}")
        }
        cachedThreadPool.shutdown()
    }

}